package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.AdverstersService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Adversters;
import com.iot08.common.entity.app.AdverstersComInfo;
import com.iot08.common.response.Response;
import com.iot08.common.utils.StringUtil;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2020/9/11 17:34
 */
@Controller
@RequestMapping("adversters")
public class AdverstersController extends BaseController {

    @Resource
    private AdverstersService adverstersService;

    /**
     * @param page
     * @time: 2020/10/27 8:57
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<Adversters> pages = adverstersService.list(page);
        mv.addObject("adversters", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("adversters/adversters_list");
        return mv;
    }

    /**
     * 添加
     *
     * @time: 2020/10/22 14:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("adversters/adversters_add");
        return mv;
    }

    /**
     * @param id
     * @param shortName
     * @time: 2020/10/26 22:36
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("checkShortName")
    public Response<Object> checkName(@RequestParam(required = false) String id, String shortName) {
        Adversters adversters = adverstersService.getByShortName(shortName);
        if (StringUtil.isBlank(id)) {
            if (adversters != null) {
                return Response.failedResponse("APP标识已存在，请修改");
            }
        } else {
            if (adversters != null && !id.equals(adversters.getId())) {
                return Response.failedResponse("APP标识已存在，请修改");
            }
        }
        return Response.successResponse();
    }

    /**
     * @param adversters
     * @time: 2020/10/26 22:36
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(Adversters adversters) {
        ModelAndView mv = new ModelAndView();
        SysUser u = this.getSessionUser();
        adversters.setCreater(u.getAccount());
        adverstersService.add(adversters, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 编辑页面
     *
     * @param id
     * @time: 2020/10/22 22:16
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("adversters", adverstersService.findById(new ObjectId(id)));
        mv.setViewName("adversters/adversters_edit");
        return mv;
    }

    /**
     * @param adversters
     * @time: 2020/10/27 8:52
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(Adversters adversters) {
        ModelAndView mv = new ModelAndView();
        adverstersService.update(adversters, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 删除
     *
     * @param [ids]
     * @time: 2020/9/14 9:41
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        return adverstersService.del(id, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * @time: 2020/10/27 10:35
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toConfig")
    public ModelAndView toConfig() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("config", adverstersService.getConfig());
        mv.setViewName("adversters/adversters_config");
        return mv;
    }

    /**
     * @param acf
     * @param request
     * @time: 2020/10/27 15:55
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "config", method = RequestMethod.POST)
    public ModelAndView config(AdverstersComInfo acf, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        SysUser u = this.getSessionUser();
        adverstersService.config(acf, request, u);
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }


}
